﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Managers.*;
    import Display.DialogUI.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Modules.stats.types.*;

    public class DailyBonusManager {

        protected var m_currentBonusDay:int;
        protected var m_previousBonusDay:int;
        protected var m_maxRecoveryDays:int;
        protected var m_maxStreak:int;
        protected var m_randomReward:String;

        public function DailyBonusManager(_arg1:Object){
            this.m_currentBonusDay = _arg1.currentBonusDay;
            this.m_previousBonusDay = _arg1.previousBonusDay;
            this.m_maxRecoveryDays = Global.gameSettings.getDailyBonusMaxRecoveryDays();
            this.m_maxStreak = Global.gameSettings.getDailyBonusMaxStreak();
            var _local2:int = (this.m_currentBonusDay - this.m_previousBonusDay);
            if (_local2 > (this.m_maxRecoveryDays + 1)){
                this.m_currentBonusDay = 1;
                this.m_previousBonusDay = 0;
            };
        }
        public function get currentBonusDay():int{
            return (this.m_currentBonusDay);
        }
        public function get previousBonusDay():int{
            return (this.m_previousBonusDay);
        }
        public function get maxStreak():int{
            return (this.m_maxStreak);
        }
        public function get randomReward():String{
            return (this.m_randomReward);
        }
        public function set randomReward(_arg1:String):void{
            this.m_randomReward = _arg1;
        }
        public function get isBonusAvailable():Boolean{
            var _local1:int = (this.m_currentBonusDay - this.m_previousBonusDay);
            var _local2 = (this.m_currentBonusDay <= 0);
            var _local3 = (this.m_currentBonusDay > this.m_maxStreak);
            var _local4 = (_local1 > (this.m_maxRecoveryDays + 1));
            var _local5 = (this.m_currentBonusDay < this.m_previousBonusDay);
            var _local6 = (_local1 == 0);
            var _local7:Boolean;
            if (((((((((_local2) || (_local3))) || (_local4))) || (_local5))) || (_local6))){
                _local7 = false;
            };
            return (_local7);
        }
        public function get isRecoveryNeeded():Boolean{
            var _local1:Boolean;
            var _local2:int = ((this.m_currentBonusDay - 1) - this.m_previousBonusDay);
            if ((((_local2 > 0)) && ((_local2 <= this.m_maxRecoveryDays)))){
                _local1 = true;
            };
            return (_local1);
        }
        public function get extraCashNeededForRecovery():int{
            var _local2:DailyBonus;
            var _local3:int;
            var _local4:int;
            var _local1:int;
            if (this.isRecoveryNeeded){
                _local2 = this.getDailyBonus(this.m_currentBonusDay);
                _local3 = _local2.recoveryCash;
                _local4 = Global.player.cash;
                _local1 = (_local3 - _local4);
                if (_local1 < 0){
                    _local1 = 0;
                };
            };
            return (_local1);
        }
        public function collectDailyBonus():void{
            if (((!(this.isBonusAvailable)) || ((this.extraCashNeededForRecovery > 0)))){
                return;
            };
            GameTransactionManager.addTransaction(new TCollectDailyBonus(this.isRecoveryNeeded));
            StatsManager.count(StatsCounterType.DAILY_BONUS, "daysClaimed", "", "", "", "", this.m_currentBonusDay);
            StatsManager.count(StatsCounterType.DAILY_BONUS, "totalClaims");
        }
        public function getDailyBonus(_arg1:int):DailyBonus{
            var _local2:XML = Global.gameSettings.getDailyBonus(_arg1);
            var _local3:DailyBonus = new DailyBonus(_local2);
            return (_local3);
        }
        public function getDailyBonusRange(_arg1:int, _arg2:int):Array{
            var _local5:DailyBonus;
            _arg1 = ((_arg1)>0) ? _arg1 : 1;
            var _local3:Array = [];
            var _local4:int = _arg1;
            while (_local4 <= _arg2) {
                if (_local4 <= this.m_maxStreak){
                    _local5 = this.getDailyBonus(_local4);
                    _local3[_local3.length] = _local5;
                } else {
                    break;
                };
                _local4++;
            };
            return (_local3);
        }
        public function isLastDay():Boolean{
            return ((this.m_currentBonusDay == this.m_maxStreak));
        }
        public function doBonusEndTasks():void{
            if (this.isLastDay()){
                UI.thawScreen();
            };
            var _local1:DailyBonusDialog = new DailyBonusDialog("", "DailyBonusRewards", 0, null, "DailyBonusRewards");
            UI.displayPopup(_local1);
            this.grantBonus();
        }
        public function grantBonus():void{
            var _local1:DailyBonus = this.getDailyBonus(this.m_currentBonusDay);
            this.m_previousBonusDay = this.m_currentBonusDay;
            if (this.m_currentBonusDay == Global.gameSettings.getDailyBonusMaxStreak()){
                if (Global.player.dailyBonusManager.randomReward){
                    Global.player.inventory.addItems(Global.player.dailyBonusManager.randomReward, 1, true);
                    StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "item", _local1.item, "", "", 1);
                } else {
                    if (_local1.coin){
                        Global.player.gold = (Global.player.gold + _local1.coin);
                        StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "coins", "", "", "", _local1.coin);
                    };
                    if (_local1.energy){
                        Global.player.updateEnergy(_local1.energy, new Array("energy", "earnings", "rewards", ""));
                        StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "energy", "", "", "", _local1.energy);
                    };
                    if (_local1.goods){
                        Global.player.commodities.add("goods", _local1.goods);
                        StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "goods", "", "", "", _local1.goods);
                    };
                    if (_local1.xp){
                        Global.player.xp = (Global.player.xp + _local1.xp);
                        StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "xp", "", "", "", _local1.xp);
                    };
                };
                return;
            };
            if (_local1.coin){
                Global.player.gold = (Global.player.gold + _local1.coin);
                StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "coins", "", "", "", _local1.coin);
            };
            if (_local1.energy){
                Global.player.updateEnergy(_local1.energy, new Array("energy", "earnings", "rewards", ""));
                StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "energy", "", "", "", _local1.energy);
            };
            if (_local1.food){
                Global.player.commodities.add("food", _local1.food);
                StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "food", "", "", "", _local1.food);
            };
            if (_local1.goods){
                Global.player.commodities.add("goods", _local1.goods);
                StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "goods", "", "", "", _local1.goods);
            };
            if (_local1.item){
                Global.player.inventory.addItems(_local1.item, 1);
                StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "item", _local1.item, "", "", 1);
            };
            if (_local1.xp){
                Global.player.xp = (Global.player.xp + _local1.xp);
                StatsManager.count(StatsCounterType.DAILY_BONUS, StatsKingdomType.BONUS_AWARDED, "xp", "", "", "", _local1.xp);
            };
        }

    }
}//package Classes 
